﻿<cfscript>
/** 
* @hint "acadmicBase - Institute"
*/ 

component 
	extends="MachII.framework.Listener" 
	displayname="acadmicManager.listeners.senate.instituteListener" 
	depends="sequenceInstituteAdmin"
	output=false 
	{
	
	/**
	* @hint "Configures this listener as part of the Mach-II framework"
	*/
	public void function configure() output=false {} 
	
	
	/**
	* @hint "撤销用户部门管理授权"
	* 
	* @event "MachII Event Object"
	*/
	public void function revokeAuthorizeGroup( required MachII.framework.Event event ) output=false {
		
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			var sql = "SELECT a.auth_id,a.institute_id  
						FROM t_institute_admin a 
						WHERE a.auth_id = :authId ";
			var queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="authId", value=event.getArg("AUTH"), cfsqltype="cf_sql_varchar" );
			var rs_depAdmin = queryObj.execute( sql=sql ).getResult();						
			
			
			if (rs_depAdmin.recordCount GT 0) {
				sql = "DELETE FROM t_institute_admin 
							WHERE auth_id = :authId ";
				queryObj = new query( datasource=application.dnsMaster );
				queryObj.addParam( name="authId", value=event.getArg("AUTH"), cfsqltype="cf_sql_varchar" );
				queryObj.execute( sql=sql );
			}
		}
		
			
		event.setArg("DepID", rs_depAdmin.institute_id);
		event.setArg("TabID", "labAdminGroup");
	}
	
	/**
	* @hint "授权用户加入部门管理团队"
	* 
	* @event "MachII Event Object"
	*/
	public void function grantAuthorizeGroup( required MachII.framework.Event event ) output=false {
	
		var rs_uniqueCheck = queryNew("auth_id");
		var sql = "";
		var queryObj="";
		var sequenceInstituteAdmin=getsequenceInstituteAdmin();
		
		/* Every SQL operation is TRANSACATION*/		
			transaction {
			
			
			if (rs_uniqueCheck.recordCount eq 0) {
				
				/* AuthID is AUTO-GEN when insert */
				 
				sql = "SELECT a.auth_id 
							FROM t_institute_admin a 
							WHERE a.institute_id = :instituteId 
								  AND
								  a.tch_id = :teacherId 
								  AND
								  a.group_id = :groupId ";
				queryObj = new Query( datasource=application.dnsMaster );
				queryObj.addParam( name="instituteId", value=event.getArg("InstituteID"), cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="teacherId", value=event.getArg("TeacherID"), cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="groupId", value=event.getArg("GroupID"), cfsqltype="cf_sql_char" );
				var rs_uniqueCheck = queryObj.execute( sql=sql ).getResult();
				 
				sequenceInstituteAdmin.setInstituteID(event.getArg("InstituteID"));
				sql = "INSERT INTO t_institute_admin a (
								   a.auth_id,
								   a.institute_id,
								   a.tch_id,
								   a.group_id 
							)
							VALUES(
								   :authId ,
								   :instituteId ,
								   :teacherId ,
								   :groupId 
							)";
				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam( name="authId", value=sequenceInstituteAdmin.getID(), cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="instituteId", value=event.getArg("InstituteID"), cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="teacherId", value=event.getArg("TeacherID"), cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="groupId", value=event.getArg("GroupID"), cfsqltype="cf_sql_char" );
				queryObj.execute(sql = sql);
				
			}
			
		}
		sql = "SELECT a.institute_id 
						FROM t_institute a 
						WHERE a.institute_id = :instituteId ";
		queryObj = new Query( datasource=application.dnsMaster );
		queryObj.addParam( name="instituteId", value=event.getArg("InstituteID"), cfsqltype="cf_sql_varchar" );
		var rs_uniqueChecks = queryObj.execute( sql=sql ).getResult();		
		event.setArg("DepID", rs_uniqueChecks.institute_id);
		event.setArg("TabID", "labAdminGroup");
	}
	
	/**
	* @hint "更新部门基础信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function updateDepartment( required MachII.framework.Event event ) output=false {
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			var sql = " SELECT a.institute_id 
						FROM t_institute a 
						WHERE a.institute_id = :InstituteID ";
			var queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="InstituteID", value=event.getArg("InstituteID"), cfsqltype="cf_sql_varchar" );
			var rs_department = queryObj.execute( sql=sql ).getResult();
			if (rs_department.recordCount GT 0) {
				
				if (event.getArg("InstitutePinyin") neq "") {
					
					 sql = "UPDATE t_institute a 
									SET 
									   a.institute_name = :InstituteName ,
									   a.institute_short = :InstituteShortName ,
									   a.institute_english = :InstituteEnglish ,
									   a.institute_address = :InstituteAddress ,
									   a.institute_pyidx = :InstitutePinyin ,
									   a.classification = :Classification  
									 WHERE
									 	a.institute_id = :InstituteID ";
					var updateObj = new Query(datasource = application.dnsMaster);
					updateObj.addParam(name ="InstituteName", value=event.getArg("InstituteName"), cfsqltype="cf_sql_varchar");
					updateObj.addParam(name ="InstituteShortName", value=event.getArg("InstituteShortName"), cfsqltype="cf_sql_varchar");
					updateObj.addParam(name ="InstituteEnglish", value=event.getArg("InstituteEnglish"), cfsqltype="cf_sql_varchar");
					updateObj.addParam(name ="InstituteAddress", value=event.getArg("InstituteAddress"), cfsqltype="cf_sql_varchar");
					updateObj.addParam(name ="InstitutePinyin", value=event.getArg("InstitutePinyin"), cfsqltype="cf_sql_varchar");							
					updateObj.addParam(name ="Classification", value=event.getArg("Classification"), cfsqltype="cf_sql_char");	
					updateObj.addParam(name ="InstituteID", value=event.getArg("InstituteID"), cfsqltype="cf_sql_varchar");	
					updateObj.execute(sql = sql);
				}
				else {
					sql = "UPDATE t_institute a 
									SET 
									   a.institute_name = :InstituteName ,
									   a.institute_short = :InstituteShortName ,
									   a.institute_english = :InstituteEnglish ,
									   a.institute_address = :InstituteAddress ,
									   a.institute_pyidx = :InstitutePinyin ,
									   a.classification = :Classification  
									 WHERE
									 	a.institute_id = :InstituteID ";
					var updateObj = new Query(datasource = application.dnsMaster);
					updateObj.addParam(name ="InstituteName", value=event.getArg("InstituteName"), cfsqltype="cf_sql_varchar");
					updateObj.addParam(name ="InstituteShortName", value=event.getArg("InstituteShortName"), cfsqltype="cf_sql_varchar");
					updateObj.addParam(name ="InstituteEnglish", value=event.getArg("InstituteEnglish"), cfsqltype="cf_sql_varchar");
					updateObj.addParam(name ="InstituteAddress", value=event.getArg("InstituteAddress"), cfsqltype="cf_sql_varchar");
					updateObj.addParam(name ="InstitutePinyin", value= getPYIDX(event.getArg("InstituteShortName")) , cfsqltype="cf_sql_varchar");							
					updateObj.addParam(name ="Classification", value=event.getArg("Classification"), cfsqltype="cf_sql_char");	
					updateObj.addParam(name ="InstituteID", value=event.getArg("InstituteID"), cfsqltype="cf_sql_varchar");	
					updateObj.execute(sql = sql);
				}

				
			}
			
		}
		sql = " SELECT a.institute_id 
						FROM t_institute a 
						WHERE a.institute_id = :InstituteID ";
		queryObj = new Query( datasource=application.dnsMaster );
		queryObj.addParam( name="InstituteID", value=event.getArg("InstituteID"), cfsqltype="cf_sql_varchar" );
		var rs_departments = queryObj.execute( sql=sql ).getResult();
		
		event.setArg("DepID", rs_departments.institute_id);
		event.setArg("TabID", "labBaseInfo");

		
	}
	
	/**
	* @hint "读取字符串的拼音首字母缩写"
	* 
	* @chineseString "中文字符串"
	*/
	private string function getPYIDX( required string chineseString ) output=false {
		
		var chineseShort = trim(chineseString);
		var char = "";
		var py = "";
		var charArray = [];
		var i = 0;
		var sql = ""; 
		var queryObj = "";
		
		if (len(chineseShort)) {
		
			for ( i=1; i lte len(chineseShort); i++) {
			
				char = mid(chineseShort, i , 1);
				sql = "SELECT charidx FROM t_chinese WHERE character = :character"; 
				
				queryObj = new Query( datasource=application.dnsSlave );
				queryObj.addParam( name="character", value=char, cfsqltype="cf_sql_char" );
				
				rs_char = queryObj.execute( sql=sql ).getResult(); 
				
				if (rs_char.recordCount) {
					py = py & rs_char.charidx;
				}
			
			}		
		}
		
		return py;
	}
	
}

</cfscript>
